<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns="http://www.w3.org/1999/xhtml"
                template="./../templates/applicationTemplate.xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:rich="http://richfaces.org/rich"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        SGA - #{bundle.playerViewTitle}
    </ui:define>

    <ui:define name="content">

        <h:form>

            <rich:panel>
                <f:facet name="header">
                    <h:outputText styleClass="mainPanel" 
                                  value="#{bundle.playerViewTitle}" />
                </f:facet>

                <rich:dataTable keepSaved="true" id="playersTable" 
                                var="record" rowKeyVar="rowKey" 
                                style="margin: 25px;" 
                                rowClasses="odd-row, even-row"
                                value="#{playerManagedBean.tableHandler.dataModel}" 
                                rows="20"
                                onrowclick="processRowClick(#{rowKey},
                                event.pageX,
                                event.pageY);"
                                onrowmouseover="jQuery(this).addClass('active-row');" 
                                onrowmouseout="jQuery(this).removeClass('active-row');" >

                    <ui:include src="./../templates/arrangeableColumnTemplate.xhtml">
                        <ui:param name="property" value="firstName" />
                        <ui:param name="bean" value="#{playerManagedBean.tableHandler}" />
                        <ui:param name="sortingEnabled" value="true"/>
                        <ui:param name="filteringEnabled" value="true"/>
                        <ui:param name="tableID" value="playersTable" />
                        <ui:param name="scrollerID" value="scroller" />
                        <ui:param name="tooltipText" value="#{bundle.columnTooltipText}" />
                        <ui:param name="columnValue" value="#{record['firstName']}" />
                    </ui:include>

                    <ui:include src="./../templates/arrangeableColumnTemplate.xhtml">
                        <ui:param name="property" value="lastName" />
                        <ui:param name="bean" value="#{playerManagedBean.tableHandler}" />
                        <ui:param name="sortingEnabled" value="true"/>
                        <ui:param name="filteringEnabled" value="true"/>
                        <ui:param name="tableID" value="playersTable" />
                        <ui:param name="scrollerID" value="scroller" />
                        <ui:param name="tooltipText" value="#{bundle.columnTooltipText}" />
                        <ui:param name="columnValue" value="#{record['lastName']}" />
                    </ui:include>

                    <ui:include src="./../templates/arrangeableColumnTemplate.xhtml">
                        <ui:param name="property" value="team" />
                        <ui:param name="bean" value="#{playerManagedBean.tableHandler}" />
                        <ui:param name="sortingEnabled" value="true"/>
                        <ui:param name="filteringEnabled" value="true"/>
                        <ui:param name="tableID" value="playersTable" />
                        <ui:param name="scrollerID" value="scroller" />
                        <ui:param name="tooltipText" value="#{bundle.columnTooltipText}" />
                        <ui:param name="columnValue" value="#{record['team'].name}" />
                    </ui:include>

                    <ui:include src="./../templates/arrangeableColumnTemplate.xhtml">
                        <ui:param name="property" value="shirtNumber" />
                        <ui:param name="bean" value="#{playerManagedBean.tableHandler}" />
                        <ui:param name="sortingEnabled" value="true"/>
                        <ui:param name="filteringEnabled" value="false"/>
                        <ui:param name="tableID" value="playersTable" />
                        <ui:param name="scrollerID" value="scroller" />
                        <ui:param name="tooltipText" value="#{bundle.columnTooltipText}" />
                        <ui:param name="columnValue" value="#{record['shirtNumber']}" />
                    </ui:include>


                    <rich:column style="text-align: center;" rendered="#{request.isUserInRole('Archiver')}" >

                        <f:facet name="header" >
                            <h:outputText class="arrangeableHeader" value="#{bundle.arrangeableTableActionsColumn}" />
                        </f:facet>

                        <a4j:commandButton image="/resources/icons/Button-Remove-icon.png" 
                                           execute="@this" render="@none"
                                           onclick="event.stopPropagation();"
                                           oncomplete="#{rich:component('deleteConfirmWindow')}.show();" 
                                           value="" styleClass="iconizedButton" >
                            <f:setPropertyActionListener target="#{playerManagedBean.currentState}" value="DELETE" />
                            <f:setPropertyActionListener target="#{playerManagedBean.currentlyManagedPlayer}" value="#{record}" />
                        </a4j:commandButton>
                        <a4j:commandButton image="/resources/icons/Change-pencil-icon.png"
                                           value="" styleClass="iconizedButton" 
                                           action="#{playerManagedBean.processActionAndRedirect('EDIT')}" >
                            <f:setPropertyActionListener target="#{playerManagedBean.currentlyManagedPlayer}" value="#{record}" />
                            <f:setPropertyActionListener target="#{playerManagedBean.playersTeam}" value="#{record.team}" />
                        </a4j:commandButton>
                    </rich:column>

                    <f:facet name="footer">
                        <rich:dataScroller id="scroller" />
                    </f:facet>
                </rich:dataTable>
            </rich:panel>

            <a4j:jsFunction name="processRowClick" execute="@this" 
                            action="#{playerManagedBean.rowKeyListener(rowKey, mX, mY)}" 
                            render="playerDetailPanel" >
                <a4j:param name="rowKey" assignTo="#{rowKey}" />
                <a4j:param name="mouseX" assignTo="#{mX}" />
                <a4j:param name="mouseY" assignTo="#{mY}" />
            </a4j:jsFunction>

            <ui:include src="./playerDetail.xhtml" >
                <ui:param name="playerDetailPanelID" value="playerDetailPanel" />
                <ui:param name="player" value="#{playerManagedBean.currentlyDetailedPlayer}" />
                <ui:param name="mouseX" value="#{playerManagedBean.mouseX}" />
                <ui:param name="mouseY" value="#{playerManagedBean.mouseY}" />
            </ui:include>

            <a4j:jsFunction name="remove" action="#{playerManagedBean.handleAction}"
                            render="playersTable" execute="@this" oncomplete="#{rich:component('deleteConfirmWindow')}.hide();" />

            <ui:include src="./../dialogs/confirmationDialogWindow.xhtml" >
                <ui:param name="confirmDialogWindowID" value="deleteConfirmWindow" />
                <ui:param name="confirmationText" value="#{bundle.deleteModalForm}" />
                <ui:param name="acceptButtonText" value="#{bundle.deleteButton}" />
                <ui:param name="acceptEventFunction" value="remove()" />
            </ui:include>

        </h:form>    
    </ui:define>

</ui:composition>
